package org.example.secondkill.dao;

import org.apache.ibatis.annotations.*;
import org.example.secondkill.domain.Order;
import org.example.secondkill.domain.vo.OrderVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrderDao {
    // 根据userId、goodsId查询订单
    @Select("select o.id,o.user_id,o.goods_id,o.status,o.create_date,o.pay_date,g.goods_name,goods_price from t_order o left join t_good g on o.goods_id=g.id where o.user_id=#{userId} and o.goods_id=#{goodsId}")
    OrderVO getOrderVOByUserIdAndGoodsId(@Param("userId") long userId, @Param("goodsId") long goodsId);

    // 插入订单
    @Insert("insert into t_order (user_id,goods_id,status,create_date) values(#{userId},#{goodsId},#{status},#{createDate});")
    int insertOrder(Order order);

    @Insert("<script>"
            + "insert into t_order (user_id,goods_id,status,create_date) values"
            + "<foreach collection=\"orderList\" item=\"order\" separator=\",\">"
            + "(#{order.userId},#{order.goodsId},#{order.status},#{order.createDate})"
            + "</foreach>"
            + "</script>")
    int insertOrders(List<Order> orderList);

    // 根据orderId查询订单
    @Select("select o.id,o.user_id,o.goods_id,o.status,o.create_date,o.pay_date,g.goods_name,goods_price from t_order o left join t_good g on o.goods_id=g.id where o.id=#{orderId}")
    OrderVO getOrderVOByOrderId(@Param("orderId") long orderId);

    // 删除所有订单
    @Delete("delete from t_order")
    void deleteAllOrder();
}
